{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyM3R38I28ULULC5fyUA7Im4",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Huangjian2013/ai-demo/blob/main/rag/06-embedding.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "MSA-bStCHdFw"
      },
      "outputs": [],
      "source": [
        "!pip install openai --quiet"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import openai\n",
        "import os\n",
        "from google.colab import userdata\n",
        "\n",
        "client = openai.OpenAI(api_key = userdata.get('OPEN_AI_KEY'),\n",
        "             base_url = userdata.get('OPEN_AI_BASE_URL'))"
      ],
      "metadata": {
        "id": "fy-DuxGLHvAU"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def embed_test(text, model):\n",
        "    response = client.embeddings.create(\n",
        "        input=text,\n",
        "        model=model\n",
        "     )\n",
        "    print(f\"长度:{len(response.data[0].embedding)}\")\n",
        "    print(response.data[0].embedding)\n",
        "\n",
        "#embed_test(\"极越01\", \"@cf/baai/bge-large-en-v1.5\")\n",
        "#embed_test(\"极越01\", \"@cf/baai/bge-base-en-v1.5\")\n",
        "\n",
        "embed_test(\"极越01\", \"@cf/baai/bge-small-en-v1.5\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MRIOnSnGHx_R",
        "outputId": "0f4ae309-982b-4948-f59d-fec973ca5359"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "长度:384\n",
            "[-0.04410106688737869, -0.030030978843569756, 0.06169069558382034, -0.05263199657201767, -0.020314065739512444, 0.018514467403292656, 0.03435991331934929, 0.01568424329161644, 0.07043807953596115, 0.021288881078362465, 0.025121057406067848, -0.10081415623426437, 0.04645279794931412, 0.0830407589673996, 0.013218442909419537, 0.002001425949856639, 0.020480230450630188, -0.08012167364358902, -0.024410417303442955, -0.03457187861204147, 0.05857245624065399, -0.09531528502702713, -0.008818479254841805, -0.07219655811786652, 0.05449233949184418, -0.013167505152523518, 0.016145437955856323, -0.037647269666194916, 0.0532795712351799, -0.09403814375400543, 0.00570245087146759, -0.03251177817583084, 0.04140717536211014, 0.03615449368953705, 0.07253970950841904, -0.016912847757339478, 0.03527408093214035, 0.01965147629380226, -0.04370741546154022, -0.010705445893108845, 0.02190796285867691, 0.03494668006896973, 0.014459596946835518, -0.008707566186785698, 0.0578913651406765, 0.0015744081465527415, -0.038948893547058105, 0.044636260718107224, 0.037637364119291306, -0.0621909461915493, -0.00598985655233264, 0.02501794323325157, 0.0216964203864336, 0.01638033241033554, 0.03772997856140137, 0.028027012944221497, 0.05880118906497955, 0.036536917090415955, 0.009002920240163803, -0.014505535364151001, 0.0171523280441761, -0.011506536044180393, -0.2110920250415802, 0.03284226730465889, 0.018190782517194748, 0.02134333737194538, 0.034493058919906616, -0.010274908505380154, 0.05077937990427017, 0.08187472075223923, 0.010204431600868702, -0.03568780794739723, 0.037608422338962555, -0.008265013806521893, -0.019633648917078972, -0.03592046722769737, 0.04778047278523445, 0.0003444939502514899, -0.03774920850992203, -0.046129561960697174, 0.018505750223994255, 0.028044305741786957, -0.02655945159494877, -0.0008942076819948852, -0.04353054240345955, 0.05266347527503967, -0.04720572009682655, -0.027592137455940247, -0.025278935208916664, -0.043374523520469666, -0.03408440575003624, 0.019886068999767303, -0.01676638238132, 0.04657435789704323, -0.09730684012174606, -0.014440206810832024, -0.019495943561196327, 0.04375888407230377, -0.037091225385665894, 0.13816837966442108, -0.07224752753973007, 0.004404671490192413, -0.0017039175145328045, 0.0013883691281080246, 0.00769429886713624, -0.003956007305532694, -0.05074644461274147, -0.10969942808151245, -0.020478129386901855, 0.022301949560642242, 0.013531937263906002, -0.04061892628669739, 0.0413888581097126, -0.03503952547907829, 0.017030611634254456, 0.05055685341358185, 0.05460862070322037, 0.003726745257154107, -0.0031220854725688696, 0.019088158383965492, 0.027527356520295143, -0.010877859778702259, 0.00601161876693368, -0.0720229223370552, -0.030547136440873146, -0.033876873552799225, 0.07878182083368301, 0.10672123730182648, 0.027312926948070526, 0.04969841614365578, -0.0044874465093016624, 0.017156127840280533, -0.051150284707546234, -0.05328810587525368, -0.008670302107930183, -0.012767412699759007, -0.06370125710964203, -0.020295899361371994, -0.028889264911413193, -0.016650373116135597, -0.011915400624275208, -0.09205164015293121, -0.03996885195374489, -0.08563007414340973, -0.04670386388897896, 0.020779205486178398, -0.03805152699351311, -0.025920674204826355, -0.0038269523065537214, -0.028673633933067322, 0.03181589022278786, 0.06402016431093216, -0.008639761246740818, -0.014095203951001167, 0.05964280664920807, -0.03457939624786377, -0.03651447594165802, 0.07829307019710541, -0.011439616791903973, -0.022938130423426628, 0.03076007589697838, -0.07094227522611618, -0.0830400362610817, 0.07181842625141144, 0.04077032953500748, -0.09978162497282028, 0.059527963399887085, 0.004927778150886297, 0.0477382093667984, -0.01918521523475647, 0.02964129112660885, 0.001764864893630147, -0.03701789304614067, 0.017725136131048203, 0.13616514205932617, 0.0054244110360741615, 0.09779519587755203, -0.061148714274168015, -0.07274742424488068, 0.03310540318489075, 0.017286548390984535, -0.01858772151172161, -0.007450655102729797, -0.01404790859669447, 0.06171391159296036, -0.023796776309609413, 0.005568955093622208, -0.007469849195331335, 0.03972986340522766, -0.020793786272406578, -0.015371597371995449, 0.06908369064331055, -0.0007413842249661684, 0.0006386543391272426, -0.0383751355111599, -0.0047422437928617, -0.022578643634915352, -0.020626598969101906, 0.032856158912181854, -0.0811600461602211, 0.14770524203777313, -0.01605316810309887, -0.013329507783055305, 0.002813088009133935, -0.011139269918203354, -0.03739403560757637, 0.022585732862353325, 0.031102929264307022, -0.024234924465417862, 0.07150477916002274, -0.0069507574662566185, -0.011651571840047836, 0.03292350843548775, -0.037213947623968124, 0.04212305694818497, -0.019344184547662735, 0.016908062621951103, 0.036930982023477554, 0.03377667814493179, 0.03577956557273865, 0.022007280960679054, 0.020888136699795723, -0.0796128660440445, -0.26771625876426697, 0.0074112736620008945, 0.03440740704536438, -0.07294861972332001, -0.00589902326464653, -0.00372908404096961, 0.020771639421582222, 0.0010367929935455322, 0.08871285617351532, 0.07340329885482788, 0.030244581401348114, 0.030514443293213844, -0.06074780970811844, 0.029319319874048233, 0.011222532019019127, 0.005402232985943556, 0.016860032454133034, 0.06904739886522293, 0.041272785514593124, -0.07837596535682678, -0.008050773292779922, 0.047115009278059006, -0.05460035800933838, -0.018352847546339035, 0.045488350093364716, -0.08287344872951508, 0.12866637110710144, 0.0770907923579216, 0.019981585443019867, 0.025759601965546608, 0.024110516533255577, -0.02144017443060875, 0.004818981513381004, -0.09627340734004974, 0.046559132635593414, 0.030528070405125618, -0.04156599938869476, -0.030644452199339867, -0.07400206476449966, -0.054128531366586685, 0.040860068053007126, 0.002976815216243267, -0.042983002960681915, -0.04064364358782768, -0.02334965579211712, -0.0616084486246109, -0.09918085485696793, -0.07117685675621033, -0.0717574805021286, 0.042304180562496185, -0.01101935189217329, -0.021625066176056862, 0.04919514060020447, 0.06379331648349762, 0.03015422448515892, -0.06294915080070496, -0.018680091947317123, -0.04004577547311783, -0.0005280794575810432, 0.004463069140911102, -0.04886089637875557, -0.00033680160413496196, 0.01992393285036087, -0.015675008296966553, 0.01731741800904274, 0.0227147676050663, -0.005180947948247194, -0.01670105569064617, 0.04505198448896408, 0.039360109716653824, -0.06372831761837006, 0.07600603997707367, 0.03162512183189392, 0.0025358686689287424, 0.03620420768857002, 0.005498089827597141, 0.0033470329362899065, -0.027726326137781143, -0.010177862830460072, -0.011785484850406647, 0.001794639858417213, -0.009848303161561489, 0.09415291994810104, 0.05398276075720787, 0.0179294403642416, 0.06229214742779732, 0.07278203219175339, 0.06183353438973427, 0.042931199073791504, -0.013349619694054127, 0.03887654468417168, 0.00111355388071388, -0.012084533460438251, -0.04680787771940231, 0.016033777967095375, -0.0070795114152133465, -0.31289803981781006, 0.020022014155983925, -0.06679926067590714, 0.005586033221334219, -0.04185370355844498, 0.045103590935468674, -0.016766486689448357, 0.02365507185459137, -0.09589525312185287, 0.029204469174146652, -0.057669445872306824, 0.06295252591371536, 0.05846347287297249, -0.050377391278743744, -0.03242576867341995, -0.044260699301958084, -0.007525257766246796, -0.03961505740880966, 0.015210861340165138, -0.05290769785642624, 0.08868575841188431, 0.026572903618216515, 0.18825088441371918, -0.019423842430114746, 0.005245936568826437, -0.020040834322571754, -0.02135036326944828, 0.00012026752665406093, -0.007090688217431307, -0.004229888319969177, -0.00013458827743306756, 0.00885726884007454, 0.0885457843542099, 0.0014424078399315476, 0.039915718138217926, 0.07000908255577087, -0.002943949308246374, -0.029174471274018288, -0.016396215185523033, 0.023784086108207703, -0.08768045157194138, 0.03802736848592758, -0.006753860507160425, -0.013891724869608879, 0.09783979505300522, 0.0167835745960474, -0.0097534554079175, -0.07258309423923492, 0.0008226078352890909, 0.004453170113265514, -0.010722177103161812, 0.027013065293431282, 0.03077651374042034, 0.03898210451006889, 0.0068001775071024895, 0.04585804045200348, 0.007429285906255245, -0.025772038847208023, -0.0024820524267852306, -0.04019585996866226, 0.04841797798871994, -0.022265838459134102, 0.028634335845708847, 0.003269038163125515, 0.003460334613919258]\n"
          ]
        }
      ]
    }
  ]
}